package com.hearwego;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import com.hearwego.DescripcionFragment.getDescripcion;
import com.hearwego.utilities.Informacion;
import com.hearwego.utilities.RssParserPull;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class TabFragmentHeader extends Fragment {
	
	ImageView imagen_header;
	TextView nombre_header, direccion_header;
	ProgressDialog dialog;
	List<Informacion> informacion;
	Drawable d;
	View view;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		new getInfoHeader().execute("");
		view = inflater.inflate(R.layout.activity_tab_fragment_header, container, false);
		return view;
	}
	
	class getInfoHeader extends AsyncTask<String, String, String>{
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = ProgressDialog.show(getActivity(), "","Cargando. Por favor espere...", true);
		}

		@Override
		protected String doInBackground(String... params) {
			Log.i("inicio", "inicio");
			RssParserPull parse = new RssParserPull("http://www.smartthinking.com.mx:8080/HearWeGo/DataServlet?Query=select%20*%20from%20pois%20where%20poi_idr=2&BaseDatos=hwg");
			informacion = null;
			informacion = parse.parse();
			Log.i("fin", "fin");


			InputStream is = null;
			try {
				is = (InputStream) new URL("http://www.smartthinking.com.mx:8080/HearWeGo/"+informacion.get(0).getFoto()).getContent();
				Log.i("URL PHOTO", "http://www.smartthinking.com.mx:8080/HearWeGo/"+informacion.get(0).getFoto());
				d = Drawable.createFromStream(is, "src name");
	 			is.close();
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
 			
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			
			imagen_header = (ImageView) view.findViewById(R.id.imagen_poi);
			imagen_header.setVisibility(View.VISIBLE);
			imagen_header.setImageDrawable(d);
			
			nombre_header = (TextView) view.findViewById(R.id.nombre_header);
			direccion_header = (TextView) view.findViewById(R.id.direccion_header);
			
			for(int i =0; i < informacion.size(); i++){
				if(informacion.get(i).getNombre() != ""){
					nombre_header.setVisibility(View.VISIBLE);
					nombre_header.setText(informacion.get(i).getNombre());
				}
				if(informacion.get(i).getDireccion() != ""){
					direccion_header.setVisibility(View.VISIBLE);
					direccion_header.setText(informacion.get(i).getDireccion());
				}
			}
			dialog.dismiss();
		}
	}
}
